package com.gitee.feizns.web.domain.result;

import com.gitee.feizns.reflect.FieldUtils;

/**
 * @author feizns
 * @since 2019/7/7
 */
public interface ErrEnums extends Err {

    String CODE_FIELD_NAME = "code";

    int DEFAULT_CODE = -1;

    String DEFAULT_MSG = "UNKNOWN";

    ErrEnums UNKNOWN = new ErrEnums() {};

    @Override
    default Integer getCode() {
        Object data = FieldUtils.getData(this, CODE_FIELD_NAME);
        if ( data != null && data instanceof Number )
            return ((Number) data).intValue();
        if ( this instanceof Enum )
            return ((Enum) this).ordinal();
        return DEFAULT_CODE;
    }

    @Override
    default String getMessage() {
        if ( this instanceof Enum )
            return ((Enum) this).name();
        return DEFAULT_MSG;
    }

}

